var vehicleInfos = [];
var branchCompanyData = [];
var chargingStationData = [];
var teamData = [];
var lineData = [];
var batteryTypeData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	$ajaxList('chargingStation', getChargingStation);
	$ajaxList('branchCompany', getBranchCompany);
	$ajaxList('batteryType', getBatteryType);
	addTitle("vehicleInfoUpdate");
	setTimeout(search, 500);
})

function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
}
function getBranchCompany(data) {
	var length = data.length;
	branchCompanyData = data;
}
function changeSubBranchCompany(){
	$("#subBranchCompanyID").empty();
	$("#subBranchCompanyID").append('<option value="">请选择</option>');
	var branchCompanyID = $("#branchCompanyID").val();
	if(branchCompanyID){
		$ajaxList('subBranchCompany', getSubBranchCompanyTable, {"branchCompanyID": branchCompanyID});
	}
}
function getSubBranchCompanyTable(data){
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#subBranchCompanyID").append("<option value='" + data[i].subBranchCompanyID + "'>" + data[i].subBranchCompanyName + "</option>");
	}
	$("#subBranchCompanyID").val(vehicleInfos[0].subBranchCompanyID);
	changeCarTeam();
}
function changeCarTeam(){
	$("#teamID").empty();
	$("#teamID").append('<option value="">请选择</option>');
	var subBranchCompanyID = $("#subBranchCompanyID").val();
	if(subBranchCompanyID){
		$ajaxList('carTeam', getTeamTable, {"subBranchCompanyID": subBranchCompanyID});
	}
}
function getTeamTable(data){
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#teamID").append("<option value='" + data[i].teamID + "'>" + data[i].teamName + "</option>");
	}
	$("#teamID").val(vehicleInfos[0].teamID);
	changeLine();
}
function changeLine(){
	$("#lineID").empty();
	$("#lineID").append('<option value="">请选择</option>');
	$("#lineID").val("");
	var teamID = $("#teamID").val();
	if (teamID) {
		$("#changeLine").show();
		$("#lineID").empty();
		$ajaxList('lineName', getLineName, {"teamID": teamID});
	} else {
		$("#changeLine").hide();
		$("#lineID").empty();
	}
}
function getLineName(data) {
	var length = data.length;
	$("#lineID").append("<option value=''>请选择</option>");
	for (var i = 0; i < length; i++) {
		$("#lineID").append("<option value='" + data[i].lineID + "'>" + data[i].lineName + "</option>");
	}
	$("#lineID").val(vehicleInfos[0].lineID);
}
function getBatteryType(data) {
	var length = data.length;
	batteryTypeData = data;
}

function search(){
	$('#CarNumber').val($("#detailID", parent.document).val());
	$ajax('searchVehicleInfo', 'searchVehicleInfoUpdate', searchVehicleInfo);
	$("#detailID", parent.document).val("");
	setTimeout(hideLoading, 500);
}
function allClick(tabFlag){
	$(".comBody").hide();
	$("#" + tabFlag).show();
}
function searchVehicleInfo(data){
	vehicleInfos = data && data.carInfoList || [];
	createComBody("detailOneBody", vehicleInfoTable, vehicleInfos[0], 1);
    createComBody("detailTwoBody", vehicleInfoTable, vehicleInfos[0], 2);
    for (var i = 0;i < chargingStationData.length;i++){
        $("#chargingStationID").append("<option value='" + chargingStationData[i].chargingStationID + "'>" + chargingStationData[i].chargingStationName + "</option>");  
	}
	for (var i = 0;i < branchCompanyData.length;i++){
        $("#branchCompanyID").append("<option value='" + branchCompanyData[i].branchCompanyID + "'>" + branchCompanyData[i].branchCompanyName + "</option>");  
	}
	for (var i = 0;i < batteryTypeData.length;i++){
        $("#batteryType").append("<option value='" + batteryTypeData[i].batteryType + "'>" + batteryTypeData[i].batteryName + "</option>");  
	}
	selectDate("useDate");
	selectDate("deliveryDate", 0, "top-right");
	$("#carVin").attr("readonly", true);
	$("#carNo").attr("readonly", true);
	$("#carNumber").attr("readonly", true);
	$("#chargingStationID").val(vehicleInfos[0].chargingStationID);
	$("#branchCompanyID").val(vehicleInfos[0].branchCompanyID);
	changeSubBranchCompany();
	$("#batteryType").val(vehicleInfos[0].batteryType);
	if(vehicleInfos[0].deleteFlag == true){
		$("#deleteFlag").val(0);
	}else{
		$("#deleteFlag").val(1);
	}
}
function updateVehicleInfo(data){
     var result = data && data.result;
     var chargingStationID = data && data.chargingStationID;
     if (result == "Success"){
    	 showMessge(messageContent["Update.Success"], undefined, goback);
     } else if (result == "NotFound"){
    	 showMessge(messageContent["Update.Failed"]);
     } else if (result == "DriverNameExist"){
         showMessge(messageContent["Car.DriverNameExist"]);
     } else if (result == "TelephoneExist"){
    	 showMessge(messageContent["Car.TelephoneExist"]);
     } else if (result == "EmailExist"){
    	 showMessge(messageContent["Car.EmailExist"]);
     } else if (result == "WeChatExist"){
    	 showMessge(messageContent["Car.WeChatExist"]);
     } else{
    	 showMessge(messageContent["Update.Error"]);
     }
}
function confirm(){
	return checkForm("saveForm", vehicleInfoTable);
}
function goback() {
	window.parent.skipMenu('vehicle/vehicleInfo', 'drownDown3', '', 'carPage', 'vehicleInfo');
}